clear all			
capture log close	
set more off 

global rawdata "D:\Data\Workdata\706991\Hours2023\rawdata\AKU"
global workdata "D:\Data\Workdata\706991\Hours2023\workdata\"

*Merge all AKU files
use "$rawdata\aku941", clear		
append using "$rawdata\aku942"
append using "$rawdata\aku943"
append using "$rawdata\aku944"

forvalues j = 5/9 { 	
  forvalues i = 1/4 {	  
  	append using "$rawdata\aku9`j'`i'"
}
} 

forvalues j = 0/3 {
  forvalues i = 1/4 {
  	append using "$rawdata\aku0`j'`i'"
}
}	

forvalues j = 4/9 {
  forvalues i = 1/4 {
  	append using "$rawdata\aauaku_0`j'`i'"
}
}	

forvalues j = 0/7 {
  forvalues i = 1/4 {
  	append using "$rawdata\aauaku_1`j'`i'"
}
}

forvalues i = 1/3 {
	append using "$rawdata\aauaku_18`i'"
}

drop v26 v49 v58 v59 v60 v61 v62 v64 v203 faktor* ftpt stapro panel virka1 hjemmarb 
drop bibesk bibstat bibvirk1 bibtimuf statusa astatus statusc a_statusc hwactua2

destring pnr, replace /*person id*/
drop if pnr == . 

*Adjust variables

*Year
gen year = .								
replace year = refaar if refaar !=.  			
replace year = v164 if v164 !=.
replace year = v162 if v162 !=.
replace year = refyear if refyear !=.
drop v164 v162 refyear refaar		

*Refweek
replace refweek = refuge if refuge !=.		
replace refweek = v166 if v166 !=.
replace refweek = v168 if v168 !=.
drop v166 v168 refuge

*hours_actual
gen h_actual = hwactual	
replace h_actual = timuf if timuf !=.	
replace h_actual = b7timuf if  b7timuf !=.			
replace h_actual = v51 if v51 !=. 			//available after 2006
replace h_actual = . if h_actual >= 97		//h>=97 is missing information 

*hours_usual
gen h_usual = hwusual	
replace h_usual = timuf if timuf !=.	
replace h_usual = b7timuf if b7timuf !=.			
replace h_usual = v51 if v51 !=. 	
replace h_usual = . if h_usual >= 97			

drop hwactual timuf b7timuf v51 

*nonstandard hours
gen night = nightwk
replace night = omnat if omnat !=.
replace night = v206 if v206 !=.
replace night = . if night == 9			
drop nightwk v206 omnat

gen evening = evenwk 
replace evening = omaften if omaften !=.
replace evening = v205 if v205 !=.
replace evening = . if evening == 9		
drop evenwk v205 omaften

gen sat = satwk
replace sat = loerdage if loerdage !=.
replace sat = v207 if v207 !=.
replace sat = . if sat == 9				
drop satwk v207 loerdage

gen sun = sunwk
replace sun = soendage if soendage !=.
replace sun = v208 if v208 !=.
replace sun = . if sun == 9				
drop sunwk v208 soendage

gen shift = shiftwk
replace shift = skifteho if skifteho !=.
replace shift = v204 if v204 !=.
replace shift = . if shift == 9				
drop shiftwk v204 skifteho

*disco
gen disco = isco4d 
replace disco = v29 if v29 !=""
replace disco = v30 if v30 !=""
gen ISCO = substr(stilaisc,1,3)
replace disco = ISCO if ISCO !=""

drop if disco == "9XX"
destring disco, replace
replace disco = . if disco == 999		
drop v29 v30 isco4d stilaisc ISCO

keep pnr h_actual h_usual year night evening sat sun shift refweek disco 

rename year aar
sort pnr aar

save "$workdata\hoursdata_1", replace

*Keep only the first observation for each person
use "$workdata\hoursdata_1", clear

sort pnr aar refweek
by pnr: gen xxx = _n
keep if xxx == 1
drop xxx

save "$workdata\hoursdata_1_X", replace

sum













